package com.company.dcm.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.crypto.SecureUtil;
import com.company.dcm.common.R;
import com.company.dcm.entity.User;
import com.company.dcm.service.UserService;
import com.company.dcm.utils.IdUtils;
import com.company.dcm.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;
import java.util.UUID;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    public R login(@RequestBody User user){
        String userName = user.getUserName();
        User data = userService.getExistByUserName(userName);
        if (data == null) {
            return R.error("账号密码错误！");
        }
        if (!data.getPassword().equals(SecureUtil.md5(user.getPassword()+data.getSalt()))) {
            return R.error("账号密码错误！");
        }
        StpUtil.login(userName);
        return R.success("登录成功！")
                .add("token",StpUtil.getTokenInfo().getTokenValue())
                .add("roleId",data.getRoleId());
    }

    @PostMapping("/register")
    public R save(@RequestBody User user){
        if(userService.getExistByUserName(user.getUserName())!=null){
            return R.error("用户名已存在！");
        }
        user.setSalt(String.valueOf(UUID.randomUUID()));
        user.setPassword(SecureUtil.md5(user.getPassword()+user.getSalt()));
        user.setUserId(IdUtils.getInstance().NextId());
        user.setRoleId(0);
        user.setStatus(1);

        if(userService.save(user)){
            return R.success("注册成功");
        }
        return R.error("注册失败");
    }

    @GetMapping("/logout")
    public R logout(){
        StpUtil.logoutByTokenValue(StpUtil.getTokenInfo().getTokenValue());
        return R.success("退出成功！");
    }

    /**
     * 重置密码
     * @param params
     * @return
     */
    @PostMapping("/rePassword")
    public R rePassword(@RequestBody Map<String, String> params) {
        //原密码,新密码,账号
        String password = params.get("password");
        String newPassword = params.get("newPassword");
        String userName = params.get("userName");

        if (StringUtils.isNullOrEmpty(password) |
                StringUtils.isNullOrEmpty(newPassword) |
                StringUtils.isNullOrEmpty(userName)) {
            return R.error("信息不全！");
        }

        User user = userService.getExistByUserName(userName);
        if (user == null) {
            return R.error("用户不存在！");
        }

        if (!Objects.equals(user.getPassword(), SecureUtil.md5(user.getPassword()+user.getSalt()))) {
            return R.error("原密码错误！");
        }

        //验证成功，进行修改，前端需重新登录。
        user.setPassword(SecureUtil.md5(newPassword + user.getSalt()));

        if (!userService.updateById(user))
            return R.error("未知修改错误！");

        return R.success("修改成功！");
    }
}
